import javax.mail.*;
import javax.mail.internet.*;

import java.util.*;


public class SendMail {

	private String smtpServer;
	private String from;
	
	public SendMail() {
		this.smtpServer = "No Server Set";
		this.from = "No Reply Address Set";
	}
	
	public void setServer(String smtpServer) {
		this.smtpServer = smtpServer;
	}
	
	public void setFrom(String from) {
		this.from = from;
	}
		/**
	    * "send" method to send the message.
		 * @throws Exception 
	    */
	public void send(String to, String subject, String body) throws Exception {
		try {
	    
	      Properties props = System.getProperties();
	      // -- Attaching to default Session, or we could start a new one --
	      props.put("mail.smtp.host", smtpServer);
	      Session session = Session.getDefaultInstance(props, null);
	      // -- Create a new message --
	      MimeMessage msg = new MimeMessage(session);
	      // -- Set the FROM and TO fields --
	      msg.setFrom(new InternetAddress(from));
	      msg.setRecipients(Message.RecipientType.TO,
	        InternetAddress.parse(to, false));
	      msg.setSubject(subject);
	      //msg.setText(body);

	      //
		      PreencodedMimeBodyPart preBody = new PreencodedMimeBodyPart("base64");
		      preBody.setText(body);
		      preBody.setHeader("Content-Language", "ru");
		      preBody.setHeader("Content-Type","text/html;charset=utf-8");
	      
	      Multipart multiPart = new MimeMultipart("mixed");
	      multiPart.addBodyPart(preBody);
	      
	      msg.setContent(multiPart);
	      //
	      
	      // -- Set some other header information --
	      //msg.setHeader("Content-Language", "ru");
	      //msg.setHeader("Content-Type","text/html;charset=utf-8");
	      //msg.setHeader("Content-Transfer-Encoding","8bit");
	      
	      //msg.setHeader("X-Priority","1");
	      //msg.setHeader("X-MSMail-Priority","High");
	      msg.setHeader("From",from);
	      msg.setHeader("Reply-To",from);
	      msg.setHeader("X-Mailer","GMailer");
	      
	      msg.setSentDate(new Date());
	      // -- Send the message --
	      Transport.send(msg);
	      System.out.println("Message sent OK for "+to);
	    }
	    catch (Exception e) {
	    	//System.out.println(e.toString());
	    	throw e;
	    }
	  }
	  
	  /*
	  public void altsend(String to, String subject, String body) {
		  try {
		  	Properties props = System.getProperties();
		    props.put("mail.smtp.host", smtpServer);
		    Session session = Session.getDefaultInstance(props, null);
		    
		    MimeMessage message = new MimeMessage(session);
		    
		    message.setFrom(new InternetAddress(from));
		    message.setRecipients(Message.RecipientType.TO,
		        InternetAddress.parse(to, false));
		    message.setSubject(subject);
		      
		    Multipart mixedMultiPart = new MimeMultipart("mixed");
		    
			    	Multipart alternativeMultiPart = new MimeMultipart("alternative");
			    
			    MimeBodyPart alternativeBodyPart = new MimeBodyPart();
			    alternativeBodyPart.setContent(alternativeMultiPart);
			    
					    BodyPart textBodyPart = new MimeBodyPart();
					    textBodyPart.setContent("Some plain text", "text/plain");
				    alternativeMultiPart.addBodyPart(textBodyPart);
				    
					    BodyPart htmlBodyPart = new MimeBodyPart();
					    htmlBodyPart.setContent("<html><body><P>Some html text</P></body></html>", "text/html");
				    alternativeMultiPart.addBodyPart(htmlBodyPart);
		    
		    mixedMultiPart.addBodyPart(alternativeBodyPart);
			    /*BodyPart csvBodyPart = new MimeBodyPart();
			    csvBodyPart.setDisposition(javax.mail.Part.ATTACHMENT);
			    csvBodyPart.setContent("Field1,Field2,Field3\n", "text/plain");
			    String fileName = "FileName_" + System.currentTimeMillis() + ".csv";
			    csvBodyPart.setFileName(fileName);
			    csvBodyPart.setHeader("Content-Type", "application/csv");
			    mixedMultiPart.addBodyPart(csvBodyPart);*/
		    
	
	/*	    message.setContent(mixedMultiPart);
		    //message.setContent(alternativeBodyPart);
		    Transport.send(message);
		    System.out.println("Message sent OK.");
	    }
	    catch (Exception ex)
	    {
	      ex.printStackTrace();
	    }
	}	*/


}
